package com.ty.blog.controller;

import com.ty.blog.common.utils.R;
import com.ty.blog.service.sys.SysCaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author zhutianyu
 * @date 2021/8/16 19:19
 * 系统controller
 */
@Slf4j
@RestController
@RequestMapping("/sys")
public class SysLoginController {

    @Resource
    private SysCaptchaService sysCaptchaService;


    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    public void getCaptcha(HttpServletResponse response, String uuid) {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);
        try {
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
            IOUtils.closeQuietly(out);
        } catch (IOException e) {
            log.error("获取图片验证码异常", e);
        }
    }

    /**
     * 登陆
     */
    @PostMapping
    public R login() {

        return R.ok();
    }


}
